package org.example.business.endpoint.http;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class HttpSocketMessageManager {

    // message(one)->socket(one)
    private Map<String, String> globalMessageSocketMap = new ConcurrentHashMap<>();

    // socket(one)->message(many)
    private Map<String, Map<String,String>> globalSocketMessageMap = new ConcurrentHashMap<>();

    public String getMessageSocket(String socketId){
        return globalMessageSocketMap.get(socketId);

    }

    public void delSocketMessageMap(String socketId){

        Map<String, String> socketMessageMap = globalSocketMessageMap.remove(socketId);

        if(null != socketMessageMap && !socketMessageMap.isEmpty()){

            socketMessageMap.forEach((key, value)->{
                globalMessageSocketMap.remove(key);

            });

        }

    }

    public void addSocketMessage(String socketId, String messageId){

        Map<String, String> socketMessageMap = globalSocketMessageMap.get(socketId);

        if(null == socketMessageMap){
            socketMessageMap = new ConcurrentHashMap<>();
            globalSocketMessageMap.put(socketId, socketMessageMap);

        }

        socketMessageMap.put(messageId, messageId);
        globalMessageSocketMap.put(messageId, socketId);

    }



}
